package com.nursinghome.admin.controller;

import com.nursinghome.admin.service.RoomService;
import com.nursinghome.bean.RespBean;
import com.nursinghome.bean.Room;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @GetMapping
    public RespBean selectAll(String pageNum, String pageSize, Room room) {

        return RespBean.ok("",roomService.selectAll(pageNum, pageSize, room));
    }
    //新增房间
    @PostMapping
    public RespBean insert(@RequestBody Room room) throws NurisingHomeException {
        if (roomService.insert(room)) {
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }
    //修改房间
    @PutMapping
    public RespBean update(@RequestBody Room room) throws NurisingHomeException {
        if (roomService.update(room)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //批量修改房间
    @PostMapping("/byRooms")
    public RespBean updateByRooms(@RequestBody List<Map<String, Integer>> rooms) throws NurisingHomeException {
        // 是否成功标志
        boolean flag = false;
        for (Map<String, Integer> map : rooms) {
            Room room = new Room();
            room.setRid(map.get("rid"));
            room.setRstate(map.get("rstate"));
            flag = roomService.update(room);
        }
        return  flag ? RespBean.ok("修改成功") : RespBean.error("修改失败");
    }
    //删除房间
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) throws NurisingHomeException {
        if (roomService.delete(id)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //删除房间
    @DeleteMapping("/byIds")
    public RespBean deleteByIds(@RequestParam("ids") Integer[] ids) throws NurisingHomeException {
        // 是否成功标志
        boolean flag = false;
        for (Integer id : ids) {
            flag = roomService.delete(id);
        }
        return flag ? RespBean.ok("删除成功") :RespBean.error("删除失败");
    }
    //回显房间数据
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id) throws NurisingHomeException {
        Room room = roomService.selectByRid(id);
        return RespBean.ok("",room);
    }
}
